package com.css.fxfzypg.zcpt.sys.controller;

import com.css.fxfzypg.modules.group.entity.UserEntity;
import com.css.fxfzypg.zcpt.sys.entity.OrgUser;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.entity.common.page.Page;
import com.css.fxfzypg.zcpt.sys.entity.vo.UpdatePasswordVO;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.base.user.viewobjects.UserRegisterVO;
import com.css.fxfzypg.constants.R;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.StringUtils;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/fxfz/suser")
@Slf4j
public class SUserController {

    @Autowired
    SUserService sUserService;

    /**
     * app账号注册
     */
    @PostMapping("/registerSUser")
    public RestResponse saveUser(@RequestBody UserRegisterVO registerVO) {
        RestResponse restResponse = null;
        OrgUser sUser = new OrgUser();
        sUser.setUserId(StringUtils.buildPrimaryId());
        try{
            restResponse = RestResponse.succeed(sUserService.addSuser(registerVO));
        }catch (Exception e){
            log.error("新增用户失败!",e);
            restResponse = RestResponse.fail("新增用户失败!");
        }
        return restResponse;
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/getUser")
    public R getUser(@RequestParam("userId") String userId) {
        //String userId = PlatformSessionContext.getUserID();
        String userJson = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        return R.ok().put("user",sUser);
    }


    /**
     * 根据组织机构id获取用户列表（分页）
     */
    @GetMapping("/queryUsersByOrg")
    public RestResponse queryUsersByOrg1(@RequestParam("orgId") String orgId,
                                        @RequestParam("userName") String userName,
                                        @RequestParam("pageSize") Integer pageSize,
                                        @RequestParam("currentPage") Integer currentPage) {
        RestResponse restResponse = null;
        if(org.apache.commons.lang3.StringUtils.isBlank(orgId)){
            restResponse = RestResponse.fail("缺失组织id!");
            return restResponse;
        }
        try{
            String jsonStr = sUserService.queryUsersByOrg1(orgId,userName,pageSize,currentPage);
            Gson gson = CreateGson.createGson();
            Page page = gson.fromJson(jsonStr, Page.class);
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            log.error("查询用户失败!",e);
            restResponse = RestResponse.fail("查询用户失败!");
        }
        return restResponse;
    }
    /**
     * 根据组织机构id获取用户列表
     */
    @GetMapping("/queryUsersByOrg1")
    public RestResponse queryUsersByOrg(@RequestParam("orgId") String orgId) {
        RestResponse restResponse = null;
        if(org.apache.commons.lang3.StringUtils.isBlank(orgId)){
            restResponse = RestResponse.fail("缺失组织id!");
            return restResponse;
        }
        try{
            String jsonStr = sUserService.queryUsersByOrg(orgId);
            Gson gson = CreateGson.createGson();
            List sysMenuList = gson.fromJson(jsonStr, List.class);
            restResponse = RestResponse.succeed(sysMenuList);
        }catch (Exception e){
            log.error("查询用户失败!",e);
            restResponse = RestResponse.fail("查询用户失败!");
        }
        return restResponse;
    }

    /**
     * 获取小组所属用户信息
     */
    @GetMapping("/getGroupUserInfo")
    public R getGroupUserInfo(@RequestParam("userId") String userId) {
        String userJson = sUserService.getGroupUserInfo(userId);
        UserEntity userEntity = CreateGson.createGson().fromJson(userJson, UserEntity.class);
        return R.ok().put("user",userEntity);
    }

    /**
     * 修改用户密码
     */
    @PostMapping("/updateUserPassword")
    public R updateUserPassword(@RequestBody UpdatePasswordVO vo)  {
        //String userId = PlatformSessionContext.getUserID();
        try{
           sUserService.updateUserPassword(vo);
        }catch (Exception e){
            return R.error(HttpStatus.SC_UNAUTHORIZED,"修改密码失败");
        }

        return R.ok(HttpStatus.SC_OK,"修改密码成功");
    }


}
